﻿using UnityEngine;
using UnityEngine.EventSystems;

public class Plant : MonoBehaviour, IDragHandler, IBeginDragHandler,IEndDragHandler
{
    public void OnBeginDrag(PointerEventData eventData)
    {
     
    }

    public void OnDrag(PointerEventData eventData)
    {
        Vector3 pos;
        RectTransformUtility.ScreenPointToWorldPointInRectangle(GetComponent<RectTransform>(), eventData.position, Camera.main, out pos);
    
        transform.position = pos;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
       
        Ray ray = RectTransformUtility.ScreenPointToRay(Camera.main, eventData.position);

        RaycastHit2D hit = Physics2D.GetRayIntersection(ray, 100.0f, 1<< LayerMask.NameToLayer("Grid"));

        if (hit.collider!=null){        
            transform.SetParent(hit.collider.transform);        
        }
        transform.localPosition = Vector2.zero;

    }
}
